package de.baumann.browser.activitys.oc

import android.app.Activity
import android.content.Intent
import android.view.View
import de.baumann.browser.R
import de.baumann.browser.adapter.OcWalletListAdapter
import de.baumann.browser.adapter.RvItemClickListener
import de.baumann.browser.base.BaseOdinActivity
import de.baumann.browser.db.ETHWallet
import de.baumann.browser.fragments.HomeWalletFragment
import de.baumann.browser.iview.ILoginView
import de.baumann.browser.present.LoginPresenter
import de.baumann.browser.utils.saveMoneyAddress
import de.baumann.browser.utils.saveWalletName
import de.baumann.browser.utils.web3j.OdinWallet
import de.baumann.browser.utils.web3j.WalletDaoUtils
import kotlinx.android.synthetic.main.activity_wallet_list.*
import kotlinx.android.synthetic.main.include_tool_bar.*


class OcWalletListActivity : BaseOdinActivity<ILoginView, LoginPresenter>(), ILoginView {

    companion object {
        fun startWalletManagerActivityForResult(context: HomeWalletFragment) {
            context.startActivityForResult(Intent(context.activity, OcWalletListActivity::class.java), 1000)
        }
    }

    private lateinit var walletListAdapter: OcWalletListAdapter

    override fun getLayout(): Int {
        OdinWallet.addAtivity(this)
        return R.layout.activity_wallet_list
    }

    override fun getTitleText(): String {
        return "钱包列表"
    }

    override fun initView() {
        initToolbar(commToolBar, commTitle)
        tvAddWallet.setOnClickListener {
            OcWalletActivity.startOcWalletActivity(mContext)
        }
        walletListAdapter = OcWalletListAdapter(mContext, object : RvItemClickListener {
            override fun onClick(position: Int) {
                val odinWallet = walletListAdapter.odinWallets!![position]
//                OcWalletDetailActivity.startOcWalletDetailActivity(mContext, odinWallet)
                walletAddress = odinWallet.address
                saveMoneyAddress(odinWallet.address)
                saveWalletName(odinWallet.name)
                presenter.login()
//                OCBActivity.startOCBActivity(mContext, odinWallet)
                setResult(Activity.RESULT_OK)
                finish()
            }
        })
        rvWallet.adapter = walletListAdapter
    }

    override fun initData() {

    }

    override fun onResume() {
        super.onResume()

        val wallets = WalletDaoUtils.loadAll()
        if (wallets != null) {
            walletListAdapter.odinWallets = wallets as ArrayList<ETHWallet>?
        }

        if (wallets.size >= 5) {
            tvAddWallet.visibility = View.GONE
        }

    }


    override fun onDestroy() {
        super.onDestroy()
        OdinWallet.removeActivity(this)
    }

    override fun showBindEth(): Boolean {
        return false
    }

    private var walletAddress: String = ""
    override fun getAccount(): String {
        return walletAddress
    }

    override fun getPassword(): String {
        return ""
    }

    override fun loginSuccess(loginName: String, licenceStatus: Int) {

    }

    override fun createPresenter(): LoginPresenter {
        return LoginPresenter()
    }

    override fun createView(): ILoginView {
        return this
    }
}
